<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/29 0029
 * Time: 14:36
 */
namespace app\api\model;

use app\common\controller\UtilCommon;
use traits\model\SoftDelete;

class Money extends Base
{
    use SoftDelete;

    /**
     * 佣金返现账单添加
     * 1.佣金在支付完成后生成佣金账单
     * 2.在确认收货后7个工作日未退货的情况下，结算佣金，否则取消佣金
     * @param int $order_id
     * @param int $order_number
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function insertInviteRecord($order_id = 0,$order_number = 0){
        //查询返佣模块是否开启
        $configService = new Config();
        $configModel = $configService->where("id = 11")->find();
        if($configModel["v"] == 0){
            return true;
        }

        //拿到账单ID
        $orderModel = [];
        if($order_id != 0){
            $where["id"] = $order_id;
            $orderModel = (new ProductOrder())->where($where)->find()->toArray();
        }

        if($order_number !== 0){
            $where["order_number"] = $order_number;
            $orderModel = (new ProductOrder())->where($where)->find()->toArray();
        }

        if(Count($orderModel) == 0){
            return false;
        }

        //获取账单用户
        $userService = new User();
        $userWhere["id"] = $orderModel["user_id"];
        $userModel = $userService->where($userWhere)->find();

        if($userModel == null || $userModel["user_type"] == 1 || $userModel["vip_time"] < time()){
            return false;
        }

        //查询返佣用户
        $beWhere["invite_code"] = $userModel["be_invite_code"];
        $beUserModel = $userService->where($beWhere)->find();
        if($beUserModel == null || $beUserModel['status'] == 2){
            return false;
        }

        //获取订单下商品返佣总额
        $orderDetailService = new ProductOrderDetail();
        $detailWhere["order_id"] = $orderModel["id"];
        $sum = $orderDetailService
            ->field("sum(invite_price*number) as invite_price_all")
            ->join("om_product","om_product_order_detail.product_id = om_product.id")
            ->where($detailWhere)
            ->find();

        //获取返佣总金额
        $invite_price_all = $sum["invite_price_all"];

        //查询返佣用户钱包
        $moneyService = new \app\api\model\Money();
        $moneyWhere["user_id"] = $beUserModel["id"];
        $moneyModel = $moneyService->where($moneyWhere)->find();
        if($moneyModel == null){
            return false;
        }

        //账单防止重复返佣
        $recordService = new MoneyRecord();
        $recodeWhere["source_order_id"] = $orderModel["id"];
        $count = $recordService->where($recodeWhere)->count();
        if($count > 0){ return true;}

        //新建返佣账单
        $model = array(
            "money_id"=>$moneyModel["id"],
            "user_id"=>$beUserModel["id"],
            "order_number"=>(new UtilCommon())->GetRandStr2(''), //不重复的流水号
            "money_type"=>0,
            "money_source_type"=>0,
            "money_source_remark"=>"佣金返现",
            "source_order_id"=>$orderModel["id"],
            "source_user_id"=>$userWhere["id"],
            "money"=>$invite_price_all,
            "create_time"=>time(),
            "pay_status"=>0,
        );

        $res = $recordService->insert($model);
        if(!$res){
            return false;
        }
        return true;
    }
}